package com.ruoyi.project.dict.material.controller;

import com.ruoyi.common.utils.JsonUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.dict.material.domain.Material;
import com.ruoyi.project.dict.material.service.IMaterialService;
import com.ruoyi.project.dict.tag.domain.MaterialTag;
import com.ruoyi.project.dict.tag.service.IMaterialTagService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


/**
 * 学习资料Controller
 *
 * @author shixiangwen
 * @date 2020-05-08
 */
@Controller
@RequestMapping("/dict/material")
public class MaterialController extends BaseController {
    private String prefix = "dict/material";

    @Autowired
    private IMaterialService materialService;

    @Autowired
    private IMaterialTagService materialTagService;

    @RequiresPermissions("dict:material:view")
    @GetMapping()
    public String material() {
        return prefix + "/material";
    }

    /**
     * 查询学习资料列表
     */
    @RequiresPermissions("dict:material:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Material material) {
        startPage();
        List<Material> list = materialService.selectMaterialList(material);
        return getDataTable(list);
    }

    /**
     * 导出学习资料列表
     */
    @RequiresPermissions("dict:material:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Material material) {
        List<Material> list = materialService.selectMaterialList(material);
        ExcelUtil<Material> util = new ExcelUtil<Material>(Material.class);
        return util.exportExcel(list, "material");
    }

    /**
     * 新增学习资料
     */
    @GetMapping("/add")
    public String add(ModelMap mmap) {

        List<MaterialTag> tags = materialTagService.selectMaterialTagList(new MaterialTag());

        mmap.put("tags", tags);

        return prefix + "/add";
    }

    /**
     * 新增保存学习资料
     */
    @RequiresPermissions("dict:material:add")
    @Log(title = "学习资料", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Material material) {
        return toAjax(materialService.insertMaterial(material));
    }

    /**
     * 修改学习资料
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        Material material = materialService.selectMaterialById(id);

        mmap.put("material", material);

        List<String> materialTags = null;
        if (!StringUtils.isEmpty(material.getTag())) {
            materialTags = JsonUtil.toListObject(material.getTag(), String.class);
        } else {
            materialTags = new ArrayList<>();
        }

        List<MaterialTag> tags = materialTagService.selectMaterialTagList(new MaterialTag());
        for (MaterialTag tag : tags) {
            if (materialTags.contains(tag.getName())) {
                tag.setFlag(true);
            }
        }
        mmap.put("tags", tags);

        return prefix + "/edit";
    }

    /**
     * 修改保存学习资料
     */
    @RequiresPermissions("dict:material:edit")
    @Log(title = "学习资料", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Material material) {
        return toAjax(materialService.updateMaterial(material));
    }

    /**
     * 审核学习资料
     */
    @RequiresPermissions("dict:material:edit")
    @Log(title = "学习资料", businessType = BusinessType.GRANT)
    @PostMapping("/approve/{id}")
    @ResponseBody
    public AjaxResult approve(@PathVariable("id") Long id, Integer approveSt) {
        Material material = materialService.selectMaterialById(id);
        material.setApproveSt(approveSt);
        return toAjax(materialService.updateMaterial(material));
    }


    /**
     * 删除学习资料
     */
    @RequiresPermissions("dict:material:remove")
    @Log(title = "学习资料", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(materialService.deleteMaterialByIds(ids));
    }

    @GetMapping("/materialTopic/{ids}")
    public String addToMaterialTopic(@PathVariable("ids") String ids, ModelMap mmap) {

        return prefix + "/materialTopic";
    }

    @RequiresPermissions("dict:material:add")
    @Log(title = "学习资料", businessType = BusinessType.INSERT)
    @PostMapping("/materialTopic")
    @ResponseBody
    public AjaxResult materialTopicSave(Long materialTopicId, String ids) {
        return toAjax(materialService.addMaterialToTopic(materialTopicId, Convert.toLongArray(ids)));
    }


    /**
     * 删除学习资料
     */
    @RequiresPermissions("dict:material:edit")
    @Log(title = "学习资料", businessType = BusinessType.GRANT)
    @PostMapping("/batchApprove")
    @ResponseBody
    public AjaxResult batchApprove(String ids) {
        return toAjax(materialService.approveMaterialByIds(ids, 1));
    }


    @GetMapping("/search")
    @ResponseBody
    public TableDataInfo search(String q) {
        startPage();
        Material material = new Material();
        material.setName(q);
        List<Material> list = materialService.selectMaterialList(material);
        return getDataTable(list);
    }
}
